<template>
  <a-modal
    :title="$t('button.add')"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :keyboard="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol" v-bind="validator.nameValidProps">
          <s-input
            v-decorator="[
              'name',
              {
                rules: [
                  {
                    required: true,
                    whitespace: true,
                    message: $t('common.emptyErr', {
                      name: '类型'
                    })
                  },
                  {
                    validator: validator.nameValidator
                  }
                ]
              }
            ]"
            :limit="128"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { asyncValidateMixin } from '@/utils/mixin'
import { SInput } from '@/components'
import { createTempType, validateTempType } from '@/api/messageCenter/mos'

export default {
  name: 'TypeForm',
  components: { SInput },
  mixins: [asyncValidateMixin],
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      },
      id: 0,
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  created () {
    this.initAsyncValidateMixin()
  },
  methods: {
    /**
     * 初始化验证器
     */
    initAsyncValidateMixin () {
      const validateFields = [
        {
          fieldName: 'name',
          caller: validateTempType,
          extraParams: {
            id: 0,
            tag: 6
          }
        }
      ]
      this.$_asyncValidateMixin_init(validateFields)
    },

    add () {
      this.visible = true
      this.$nextTick(() => {
        const model = { name: '' }
        this.form.setFieldsValue(model)
      })
    },

    handleSubmit () {
      if (!this.canSubmit) {
        return
      }
      const {
        form: { validateFields, getFieldsValue }
      } = this
      this.confirmLoading = true
      const fieldNames = this.$_asyncValidateMixin_fieldsFilter(getFieldsValue())
      validateFields(fieldNames, (errors, values) => {
        if (!errors) {
          const { name } = getFieldsValue()
          const model = {
            name: name.trim()
          }
          // 新增
          createTempType(model)
            .then(res => {
              this.visible = false
              this.$message.success(res.message, 1)
              this.resetForm()
              this.$emit('ok', Object.assign({}, model))
            })
            .catch(err => {})
            .finally(() => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },

    handleCancel () {
      this.visible = false
      this.resetForm()
    },

    resetForm () {
      this.id = 0
      const originalModel = { name: '' }
      this.form.setFieldsValue(originalModel)
    }
  }
}
</script>

<style></style>
